package com.zhentao.map.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhentao.pojo.WaterQualityData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 设备数据访问层
 */
@Mapper
public interface MapDeviceMapper extends BaseMapper<WaterQualityData> {

    /**
     * 获取设备最新水质数据
     * @param deviceId 设备ID
     * @return 最新水质数据
     */
    @Select("SELECT * FROM water_quality_data WHERE device_id = #{deviceId} ORDER BY collect_time DESC LIMIT 1")
    WaterQualityData getLatestWaterQualityData(@Param("deviceId") String deviceId);

    /**
     * 获取设备历史数据
     * @param deviceId 设备ID
     * @param limit 限制条数
     * @return 历史数据列表
     */
    @Select("SELECT * FROM water_quality_data WHERE device_id = #{deviceId} ORDER BY collect_time DESC LIMIT #{limit}")
    List<WaterQualityData> getWaterQualityHistory(@Param("deviceId") String deviceId, @Param("limit") Integer limit);

    /**
     * 获取设备统计数据
     * @param deviceId 设备ID
     * @return 统计数据
     */
    @Select("SELECT " +
            "AVG(ph_value) as avg_ph, " +
            "AVG(temperature) as avg_temp, " +
            "AVG(turbidity) as avg_turbidity, " +
            "COUNT(*) as total_count " +
            "FROM water_quality_data WHERE device_id = #{deviceId}")
    Map<String, Object> getDeviceStatistics(@Param("deviceId") String deviceId);
}
